package cn.study.controller;


import cn.study.entity.StudyIntegral;
import cn.study.entity.StudyVideo;
import cn.study.service.StudyEmployeeService;
import cn.study.service.StudyIntegralService;
import cn.study.service.StudyVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 视频表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@RequestMapping("/study-video")
public class StudyVideoController {
    @Resource
    StudyVideoService studyVideoService;

    @Autowired
    StudyIntegralService studyIntegralService;

    @Autowired
    StudyEmployeeService studyEmployeeService;

    @GetMapping("/get/{id}")
    public String video(Model model, @PathVariable(value = "id") Integer id) {
        StudyVideo studyVideo=studyVideoService.getById(id);
        model.addAttribute("studyVideo", studyVideo);
        return "video";
    }

    @ResponseBody
    @GetMapping("/vsignin/{empid}")
    public String vsignin(@PathVariable("empid") Integer empid){
        /*先看看积分表里有没有员工
         * 传入员工id判断
         * */
        QueryWrapper<StudyIntegral> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("integral_emp_id",empid);
        Integer count=studyIntegralService.count(queryWrapper);
        if(count==0) {
            StudyIntegral studyIntegral = new StudyIntegral();
            studyIntegral.setIntegralEmpId(empid);
            studyIntegralService.save(studyIntegral);
        }
        /*传入积分类
         * 判断员工id
         *
         * */
        QueryWrapper<StudyIntegral> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("integral_emp_id",empid);
        StudyIntegral studyIntegral2 = studyIntegralService.getOne(queryWrapper1);
        int i=studyIntegral2.getIntegral()+5;
        studyIntegral2.setIntegral(i);
        studyIntegralService.update(studyIntegral2,queryWrapper);
        return "视频播放结束，奖励积分5点";
    }
}

